package com.daidai.source.mocksource.source;

import com.daidai.source.mocksource.domain.Order;
import org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction;

import java.util.Random;
import java.util.UUID;

public class MyOrderSource extends RichParallelSourceFunction<Order> {

    boolean flag = true;

    @Override
    public void run(SourceContext<Order> sourceContext) throws Exception {
        Random random = new Random();
        while (flag){
            Thread.sleep(1000);
            String id = UUID.randomUUID().toString();
            int userId = random.nextInt(3);
            int money = random.nextInt(101);
            long createTime = System.currentTimeMillis();
            sourceContext.collect(new Order(id, userId, money, createTime));
        }
    }

    @Override
    public void cancel() {
        flag = false;
    }
}
